package gui;

import gamemngr.GameMngr;
import gamemngr.HelpMenu;
import gamemngr.GameMngr.GameStatus;
import logic.Country;
import logic.moves.AttackMove;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;

import resources.images.ImagesPath;

import com.swtdesigner.SWTResourceManager;

import java.util.Random;

public class AttackDialog extends Dialog {

	private Label moveSoldiersLabel;
	private Scale scale;
	private Button rollButton;
	private Label redDie2;
	private Label redDie1;
	private Label blueDie3;
	private Label blueDie2;
	private Label blueDie1;
	private Label attackedLabel;
	private Label attackerLabel;
	private Label attackedArmyLabel;
	private Label attackingArmyLabel;
	protected Shell shell;
	private RiskGUI gui;
	private GameMngr game;
	
	private Country attackingCountry;
	private Country attackedCountry;
	private boolean isAttacked = false;
	private Integer attackedNumOfSoldiers;
	private Integer attackingNumOfSoldiers;
	
	private AttackMove attack;
	private Label aliveUnits;
	private Label deadUnits;
	private Label defenderImage;
	private Label attackingArmyLabel2;
	private Label attackedArmyLabel2;
	
	private Label diceLabel;
	
	private Random rand = new Random(System.currentTimeMillis());
	private boolean isNetwork;

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public AttackDialog(Shell parent, int style, boolean isNetwork) {
		super(parent, style);
		this.isNetwork = isNetwork;
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public AttackDialog(Shell parent, boolean isNetwork) {
		this(parent, SWT.NONE, isNetwork);
	}
	
	public AttackDialog(Shell parent, Country attackingCountry, Country attackedCountry, RiskGUI gui, GameMngr game, boolean isNetwork) {
		this(parent, SWT.NONE, isNetwork);
		this.attackedCountry = attackedCountry;
		this.attackingCountry = attackingCountry;
		this.gui = gui;
		this.game = game;
		this.attackedNumOfSoldiers = attackedCountry.getSoldiers();
		this.attackingNumOfSoldiers = attackingCountry.getSoldiers();
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		if(attack != null)
			return attack.getBriefDo();
		else
			return "\n";
	}
	
	
	private void rollDiceEffect() {
		
		Integer rolls = 3;
		Integer delay = 100;
		
		while (rolls > 0) {
			
			if(this.attack.getAttackerStrikes().length >= 1){
				//this.blueDie1.setImage(new Image(Display.getDefault(),ImagesPath.getImageStream("blue" + Integer.toString(6-rand.nextInt(6)) + ".png")));
				this.blueDie1.setImage(Images.getImage("blue" + Integer.toString(6-rand.nextInt(6)) + ".png"));
				this.blueDie1.setVisible(true);
			}else{
				this.blueDie1.setVisible(false);
			}		
			if(this.attack.getAttackerStrikes().length >= 2){
				//this.blueDie2.setImage(new Image(Display.getDefault(),ImagesPath.getImageStream("blue" + Integer.toString(6-rand.nextInt(6)) + ".png")));
				this.blueDie2.setImage(Images.getImage("blue" + Integer.toString(6-rand.nextInt(6)) + ".png"));
				this.blueDie2.setVisible(true);
			}else{
				this.blueDie2.setVisible(false);
			}		
			if(this.attack.getAttackerStrikes().length >= 3){
				//this.blueDie3.setImage(new Image(Display.getDefault(),ImagesPath.getImageStream("blue" + Integer.toString(6-rand.nextInt(6)) + ".png")));
				this.blueDie3.setImage(Images.getImage("blue" + Integer.toString(6-rand.nextInt(6)) + ".png"));
				this.blueDie3.setVisible(true);
			}else{
				this.blueDie3.setVisible(false);
			}
			if(this.attack.getDefenderStrikes().length >= 1){
				//this.redDie1.setImage(new Image(Display.getDefault(),ImagesPath.getImageStream("red" + Integer.toString(6-rand.nextInt(6)) + ".png")));
				this.redDie1.setImage(Images.getImage("red" + Integer.toString(6-rand.nextInt(6)) + ".png"));
				this.redDie1.setVisible(true);
			}else{
				this.redDie1.setVisible(false);
			}		
			if(this.attack.getDefenderStrikes().length >= 2){
				//this.redDie2.setImage(new Image(Display.getDefault(),ImagesPath.getImageStream("red" + Integer.toString(6-rand.nextInt(6)) + ".png")));
				this.redDie2.setImage(Images.getImage("red" + Integer.toString(6-rand.nextInt(6)) + ".png"));
				this.redDie2.setVisible(true);
			}else{
				this.redDie2.setVisible(false);
			}
			
			diceLabel.redraw();
			
			this.blueDie1.redraw();
			this.blueDie2.redraw();
			this.blueDie3.redraw();
			this.redDie1.redraw();
			this.redDie2.redraw();
			
			rolls--;

			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {}
		
		}
	}		
	
	private void rollDiceGUI() {
		if(this.attack.getAttackerStrikes().length >= 1){
			this.blueDie1.setImage(Images.getImage("blue" + this.attack.getAttackerStrikes()[0] + ".png"));
			this.blueDie1.setVisible(true);			
		}else{
			this.blueDie1.setVisible(false);
		}		
		if(this.attack.getAttackerStrikes().length >= 2){
			this.blueDie2.setImage(Images.getImage("blue" + this.attack.getAttackerStrikes()[1] + ".png"));
			this.blueDie2.setVisible(true);
		}else{
			this.blueDie2.setVisible(false);
		}		
		if(this.attack.getAttackerStrikes().length >= 3){
			this.blueDie3.setImage(Images.getImage("blue" + this.attack.getAttackerStrikes()[2] + ".png"));
			this.blueDie3.setVisible(true);
		}else{
			this.blueDie3.setVisible(false);
		}
		
		if(this.attack.getDefenderStrikes().length >= 1){
			this.redDie1.setImage(Images.getImage("red" + this.attack.getDefenderStrikes()[0] + ".png"));
			this.redDie1.setVisible(true);
		}else{
			this.redDie1.setVisible(false);
		}		
		if(this.attack.getDefenderStrikes().length >= 2){
			this.redDie2.setImage(Images.getImage("red" + this.attack.getDefenderStrikes()[1] + ".png"));
			this.redDie2.setVisible(true);
		}else{
			this.redDie2.setVisible(false);
		}
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(388, 384);
		shell.setText("Attack");
		
		defenderImage = new Label(shell, SWT.NONE);
		defenderImage.setBounds(244, 38, 90, 74);
		defenderImage.setImage(SWTResourceManager.getImage(AttackDialog.class, "/resources/images/defender.JPG"));
		
		deadUnits = new Label(shell, SWT.NONE);
		deadUnits.setBounds(182, 75, 26, 28);
		deadUnits.setImage(SWTResourceManager.getImage(AttackDialog.class, "/resources/images/tombstone.GIF"));
		deadUnits.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		aliveUnits = new Label(shell, SWT.NONE);
		aliveUnits.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		aliveUnits.setImage(SWTResourceManager.getImage(AttackDialog.class, "/resources/images/aliveUnits.JPG"));
		aliveUnits.setBounds(180, 41, 26, 28);
		{
			Label attackerImage = new Label(shell, SWT.NONE);
			attackerImage.setImage(SWTResourceManager.getImage(AttackDialog.class, "/resources/images/attacker.JPG"));
			attackerImage.setBounds(46, 38, 90, 74);
		}
		shell.addShellListener(new ShellAdapter() {
			public void shellClosed(final ShellEvent arg0) {
				if(isAttacked && attack.isAttackWon())
					attack.moveSoldiersOnConquer(isNetwork, game);
				}
			});

		attackingArmyLabel = new Label(shell, SWT.CENTER);
		attackingArmyLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		attackingArmyLabel.setFont(SWTResourceManager.getFont("", 12, SWT.BOLD));
		attackingArmyLabel.setText(String.valueOf(attackingCountry.getSoldiers()));
		//attackingArmyLabel.setBackground(attackingCountry.getOwner().getColour());
		attackingArmyLabel.setBounds(154, 47, 19, 20);
		attackingArmyLabel.setForeground(attackingCountry.getOwner().getColour());
		
		attackingArmyLabel2 = new Label(shell, SWT.CENTER);
		attackingArmyLabel2.setBounds(154, 79, 19, 20);
		attackingArmyLabel2.setText("0");
		attackingArmyLabel2.setFont(SWTResourceManager.getFont("", 12, SWT.BOLD));
		attackingArmyLabel2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		attackingArmyLabel2.setForeground(attackingCountry.getOwner().getColour());
		
		attackedArmyLabel = new Label(shell, SWT.CENTER);
		attackedArmyLabel.setFont(SWTResourceManager.getFont("", 12, SWT.BOLD));
		attackedArmyLabel.setBounds(211, 47, 19, 20);
		attackedArmyLabel.setText(String.valueOf(attackedCountry.getSoldiers()));
		attackedArmyLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		attackedArmyLabel.setForeground(attackedCountry.getOwner().getColour());
		
		attackedArmyLabel2 = new Label(shell, SWT.CENTER);
		attackedArmyLabel2.setBounds(211, 79, 19, 20);
		attackedArmyLabel2.setText("0");
		attackedArmyLabel2.setFont(SWTResourceManager.getFont("", 12, SWT.BOLD));
		attackedArmyLabel2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		attackedArmyLabel2.setForeground(attackedCountry.getOwner().getColour());
		
		attackerLabel = new Label(shell, SWT.CENTER);
		attackerLabel.setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
		attackerLabel.setBounds(40, 20, 150, 16);
		attackerLabel.setText(this.attackingCountry.getOwner().getPlayerName() + " - " + this.attackingCountry.getName());
		attackerLabel.setForeground(attackingCountry.getOwner().getColour());
		attackerLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

		attackedLabel = new Label(shell, SWT.CENTER);
		attackedLabel.setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
		attackedLabel.setBounds(194, 20, 150, 16);
		attackedLabel.setText(this.attackedCountry.getOwner().getPlayerName() + " - " + this.attackedCountry.getName());
		attackedLabel.setForeground(attackedCountry.getOwner().getColour());
		attackedLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

		blueDie1 = new Label(shell, SWT.NONE);
		blueDie1.setBounds(83, 132, 40, 40);
		blueDie1.setVisible(false);

		blueDie2 = new Label(shell, SWT.NONE);
		blueDie2.setBounds(83, 177, 40, 40);
		blueDie2.setVisible(false);

		blueDie3 = new Label(shell, SWT.NONE);
		blueDie3.setBounds(83, 222, 40, 40);
		blueDie3.setVisible(false);

		redDie1 = new Label(shell, SWT.NONE);
		redDie1.setBounds(261, 132, 40, 40);
		redDie1.setVisible(false);

		redDie2 = new Label(shell, SWT.NONE);
		redDie2.setBounds(261, 177, 40, 40);
		redDie2.setVisible(false);
		
		diceLabel = new Label(shell, SWT.CENTER | SWT.SHADOW_IN | SWT.BORDER);
		diceLabel.setImage(SWTResourceManager.getImage(AttackDialog.class, "/resources/images/battlefield.jpg"));
		diceLabel.setBounds(38, 118, 307, 156);

		rollButton = new Button(shell, SWT.NONE);
		rollButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				attack = new AttackMove(attackingCountry, attackedCountry, isNetwork, game);
				isAttacked = true;
				attackingCountry.getOwner().addMove(attack, isNetwork);
				rollDiceEffect();
				rollDiceGUI();
				game.status = GameStatus.GAME_PLAY;
				attackedArmyLabel.setText(String.valueOf(attack.getDefendSoldiersLeft()));
				attackingArmyLabel.setText(String.valueOf(attack.getAttackSoldiersLeft()));
				attackedArmyLabel2.setText(String.valueOf(attackedNumOfSoldiers-attack.getDefendSoldiersLeft()));
				attackingArmyLabel2.setText(String.valueOf(attackingNumOfSoldiers-attack.getAttackSoldiersLeft()));
				if(attack.isBattleFinished()){
					rollButton.setEnabled(false);
					if(attack.isAttackWon()){
						int min = attack.getMoveSoldiers();// number of dice rolled (effective in battle)
						int max = attack.getAttackSoldiersLeft()-1;
						scale.setMaximum(max);
						moveSoldiersLabel.setText(String.valueOf(min));
						moveSoldiersLabel.setVisible(true);
						if(min < max) //bug fix, apparently, min MUST be < than max.
						{
							scale.setMinimum(min);
							scale.setEnabled(true);
						}
					}
				}
				gui.updateGUIMap();
				// reached max limit of moves per turn (house rules)
				if(attackingCountry.getOwner().CheckMovesRule()) {
					if(attack.isAttackWon())attack.moveSoldiersOnConquer(isNetwork, game);
					gui.setMoveRuleOnAttack(true);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					shell.dispose();
				}
			}
		});
		rollButton.setText("Roll");
		rollButton.setBounds(121, 318, 44, 23);

		final Label battleLabel = new Label(shell, SWT.BORDER);
		battleLabel.setBounds(38, 16, 307, 98);
		battleLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

		final Button endButton = new Button(shell, SWT.NONE);
		endButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				if(isAttacked && attack.isAttackWon()){
					attack.moveSoldiersOnConquer(isNetwork, game);
				}
				shell.dispose();
			}
		});
		endButton.setText("End Attack");
		endButton.setBounds(168, 318, 64, 23);

		scale = new Scale(shell, SWT.NONE);
		scale.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				moveSoldiersLabel.setText(String.valueOf(scale.getSelection()));
				attack.setMoveSoldiers(scale.getSelection());
			}
		});
		scale.setEnabled(false);
		scale.setBounds(106, 276, 170, 42);

		moveSoldiersLabel = new Label(shell, SWT.NONE);
		moveSoldiersLabel.setFont(SWTResourceManager.getFont("", 16, SWT.NONE));
		moveSoldiersLabel.setText("1");
		moveSoldiersLabel.setVisible(false);
		
		moveSoldiersLabel.setBounds(275, 285, 37, 31);

		final Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				HelpMenu help = new HelpMenu("AttackHelp.txt");
				MessageDialog msg = new MessageDialog(shell,"Help",null,help.toString(),MessageDialog.QUESTION,new String[] {"OK"},1);
				msg.open();
			}
		});
		button.setFont(SWTResourceManager.getFont("", 10, SWT.BOLD));
		button.setText("?");
		button.setBounds(238, 318, 26, 23);
			
		//
	}
}
